//
//  WPReadingRankingSubCommentVC.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/29.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPReadingRankingSubCommentVC.h"
#import "WPNewCommentModel.h"
#import "WPNewCommentCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WPCommentBtn.h"
#import "WPMinePlaceholderTextView.h"

@interface WPReadingRankingSubCommentVC ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property(nonatomic,strong)UIView *navBarView;

@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UIButton *praiseBtn;
@property(nonatomic,strong)UILabel *praiseCount;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UILabel *timeLabel;

@property(nonatomic,strong)UITableView *bottomTableView;

@property(nonatomic,strong)UIView *textViewBgView;
@property(nonatomic,strong)WPMinePlaceholderTextView *textView;
@property(nonatomic,strong)UIButton *coverBtn;
@property(nonatomic,strong)UIButton *composeBtn;
@property(nonatomic,strong)UIButton *sendBtn;
//没有评论的界面
@property(nonatomic,strong)UIView *noCommentView;

@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)NSMutableArray *models;
@property(nonatomic,assign)int indexPage;

@property(nonatomic,assign)NSInteger selectedIndex;
@property(nonatomic,assign)BOOL isSpecialReply;
@property(nonatomic,assign)BOOL isShow;

@end

@implementation WPReadingRankingSubCommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    
    [self setupUI];
    
}

-(void)setupUI
{
    [self navBarView];
    [self setupMainComment];
    [self bottomTableView];
    [self bottomView];
//    [self noCommentView];
}

-(UIView *)navBarView
{
    if (_navBarView == nil)
    {
        _navBarView = [UIView new];
        [self.view addSubview:_navBarView];
        
        [_navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(20);
            make.height.mas_equalTo(44);
        }];
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"详情";
        titleLabel.font = [UIFont systemFontOfSize:18];
        titleLabel.textColor = GLobalFontColor;
        [_navBarView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_navBarView);
        }];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
        [backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_navBarView addSubview:backBtn];
        
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_navBarView);
            make.left.equalTo(self.navBarView).offset(15);
        }];
        
        UIImageView *lineView = [UIImageView lineImageView];
        [_navBarView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_navBarView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _navBarView;
}

-(void)setupMainComment
{
    [self iconView];
    [self nameLabel];
    [self contentLabel];
    [self timeLabel];
    [self praiseBtn];
    [self praiseCount];
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        UIImage *image = [UIImage imageNamed:@"comment-default_icon"];
        _iconView = [[UIImageView alloc] initWithImage:image];
        
        _iconView.layer.cornerRadius = image.size.width*0.5;
        _iconView.layer.masksToBounds = YES;
        
        [self.view addSubview:_iconView];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(15);
            make.top.equalTo(self.navBarView.mas_bottom).offset(20);
            make.width.height.mas_equalTo(image.size.width);
        }];
    }
    return _iconView;
}

-(UILabel *)nameLabel
{
    if (_nameLabel == nil)
    {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = RGB_SAME(102);
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.text = self.model.userName;
        [_nameLabel sizeToFit];
        [self.view addSubview:_nameLabel];
        
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconView);
            make.left.equalTo(self.iconView.mas_right).offset(15);
        }];
    }
    return _nameLabel;
}

-(UILabel *)contentLabel
{
    if (_contentLabel == nil)
    {
        _contentLabel = [UILabel new];
        _contentLabel.numberOfLines = 0;
        _contentLabel.text = self.model.content;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = GLobalFontColor;
        [self.view addSubview:_contentLabel];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel);
            make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
            make.right.equalTo(self.view).offset(-15);
        }];
    }
    return _contentLabel;
}

-(UILabel *)timeLabel
{
    if (_timeLabel == nil)
    {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = RGB_SAME(102);
        [_timeLabel sizeToFit];
        _timeLabel.text = [[WPDateFormatterManager sharedManager] wbwyTimeStrDescriptionWithTimeStampStr:[NSString stringWithFormat:@"%zd",self.model.time]];
        [self.view addSubview:_timeLabel];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentLabel);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            
        }];
        
    }
    return _timeLabel;
}

-(UILabel *)praiseCount
{
    if (_praiseCount == nil)
    {
        _praiseCount = [UILabel new];
        _praiseCount.textColor = RGB_SAME(183);
        _praiseCount.font = [UIFont systemFontOfSize:14];
        _praiseCount.text = [NSString stringWithFormat:@"%zd",self.model.praiseAmount];
        _praiseCount.textAlignment = NSTextAlignmentRight;
        [self.view addSubview:_praiseCount];
        
        [_praiseCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.right.equalTo(self.view).offset(-15);
        }];
    }
    return _praiseCount;
}

-(UIButton *)praiseBtn
{
    if (_praiseBtn == nil)
    {
        _praiseBtn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"reading_praise_normal"] forState:UIControlStateNormal];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"reading_praise_selected"] forState:UIControlStateSelected];
        _praiseBtn.selected = self.model.isPraised;
        [_praiseBtn addTarget:self action:@selector(praiseBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_praiseBtn];
        
        [_praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.right.equalTo(self.praiseCount.mas_left).offset(-5);
            
        }];
    }
    return _praiseBtn;
}

-(UITableView *)bottomTableView
{
    if (_bottomTableView == nil)
    {
        _bottomTableView = [[UITableView alloc] init];
        _bottomTableView.backgroundColor = RGB_SAME(245);
        
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        headerView.backgroundColor = GlobalBgColor;
        _bottomTableView.tableHeaderView = headerView;
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"全部评论";
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textColor = GLobalFontColor;
        [headerView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView).offset(15);
            make.left.equalTo(headerView).offset(15);
        }];
        
        _bottomTableView.delegate = self;
        _bottomTableView.dataSource = self;
        _bottomTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_bottomTableView registerClass:[WPNewCommentCell class] forCellReuseIdentifier:@"WPNewCommentCell"];
        [self.view addSubview:_bottomTableView];
        
        [_bottomTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.timeLabel.mas_bottom).offset(15);
            make.bottom.equalTo(self.view).offset(-49);
        }];
        
        __weak typeof(self) weakself = self;
        MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            //      刷新数据
            [weakself refreshData];
        }];
        _bottomTableView.mj_header = mj_header;
        [mj_header setTitle:@"下拉可以刷新" forState:MJRefreshStateIdle];
        [mj_header setTitle:@"松开立即刷新" forState:MJRefreshStatePulling];
        [mj_header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
        mj_header.stateLabel.textColor = GLobalFontColor;
        mj_header.lastUpdatedTimeLabel.textColor = GLobalFontColor;
        
        //    上拉刷新控件
        MJRefreshAutoFooter *mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            [weakself loadMoreData];
            [mj_footer endRefreshing];
        }];
        _bottomTableView.mj_footer = mj_footer;
        mj_footer.automaticallyRefresh = NO;
        mj_footer.automaticallyHidden = NO;
    }
    return _bottomTableView;
}

-(UIView *)bottomView
{
    if (_bottomView == nil)
    {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_bottomView];
        
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self.view);
            make.top.equalTo(self.bottomTableView.mas_bottom);
        }];
        
        UIImageView *lineView = [[UIImageView alloc] init];
        lineView.backgroundColor = GLobalLineColor;
        [_bottomView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(_bottomView);
            make.height.mas_equalTo(0.5);
        }];
        
        WPCommentBtn *commentBtn = [[WPCommentBtn alloc] init];
        [commentBtn setBackgroundImage:[UIImage imageNamed:@"reading_fieldBgImage"] forState:UIControlStateNormal];
        [commentBtn setTitle:@"写评论...." forState:UIControlStateNormal];
        commentBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        [commentBtn setTitleColor:GLobalFontColor forState:UIControlStateNormal];
        [commentBtn addTarget:self action:@selector(commentBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        commentBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.bottomView addSubview:commentBtn];
        
        [commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bottomView);
            make.left.equalTo(self.bottomView).offset(15);
            make.right.equalTo(self.bottomView).offset(-15);
            make.height.mas_equalTo(28);
        }];
    }
    return _bottomView;
}

-(UIView *)noCommentView
{
    if (_noCommentView == nil)
    {
        _noCommentView = [UIView new];
        _noCommentView.backgroundColor = GlobalBgColor;
        [self.view addSubview:_noCommentView];
        
        [_noCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bottomTableView);
        }];
        
        UIImageView *noCommentImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_comment"]];
        [noCommentImageView sizeToFit];
        [_noCommentView addSubview:noCommentImageView];
        
        [noCommentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noCommentView);
            make.top.equalTo(_noCommentView).offset(80*kMoreDetailCaseScale);
        }];
        
        UILabel *noCommentLabel = [UILabel new];
        noCommentLabel.text = @"暂无其他评论";
        noCommentLabel.font = [UIFont systemFontOfSize:14];
        noCommentLabel.textColor = GloballightFontColor;
        [_noCommentView addSubview:noCommentLabel];
        
        [noCommentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noCommentView);
            make.top.equalTo(noCommentImageView.mas_bottom).offset(20*kMoreDetailCaseScale);
        }];
    }
    return _noCommentView;
}

-(UIView *)textViewBgView
{
    if (_textViewBgView == nil)
    {
        
        _textViewBgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, SCREEN_WIDTH, 145)];
        _textViewBgView.backgroundColor = GlobalBgColor;
        [self.view addSubview:_textViewBgView];
    }
    return _textViewBgView;
}

-(WPMinePlaceholderTextView *)textView
{
    if (_textView == nil)
    {
        _textView = [[WPMinePlaceholderTextView alloc] init];
        _textView.placeholder = @"有价值的评论将会被优先展示(400字以内)";
        _textView.font = [UIFont systemFontOfSize:14];
        _textView.textColor = GLobalFontColor;
        _textView.layer.cornerRadius = 4;
        _textView.layer.masksToBounds = YES;
        _textView.delegate = self;
        [self.textViewBgView addSubview:_textView];
        
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self.textViewBgView).offset(10);
            make.right.equalTo(self.textViewBgView).offset(-10);
            make.height.mas_equalTo(90);
        }];
    }
    return _textView;
}

-(UIButton *)sendBtn
{
    if (_sendBtn == nil)
    {
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendBtn setBackgroundImage:[UIImage imageNamed:@"reading_sendBtn_disable"] forState:UIControlStateDisabled];
        [_sendBtn setBackgroundImage:[UIImage imageNamed:@"reading_sendBtn_enable"] forState:UIControlStateNormal];
        _sendBtn.enabled = NO;
        [_sendBtn addTarget:self action:@selector(sendBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.textViewBgView addSubview:_sendBtn];
        
        [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.textViewBgView).offset(-10);
            make.top.equalTo(self.textView.mas_bottom).offset(10);
        }];
    }
    return _sendBtn;
}

-(void)setModel:(WPNewCommentModel *)model
{
    _model = model;
    
    NSString *url = @"appraise/findAppraise.do";
    NSDictionary *dict = @{@"recordId":@(self.recordId),@"id":@(model.id),@"page":@(0)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //        NSLog(@"子评论:%@",responseObject);
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        if ([responseDict[@"result"] isEqualToString:@"success"])
        {
            NSArray *arr = responseDict[@"detail"];
            
            //有评论的时候
            if (arr.count)
            {
                //            不满一页影藏底部刷新控件
                if (arr.count < 10)
                {
                    self.bottomTableView.mj_footer.hidden = YES;
                }
                
                for (NSDictionary *dict in arr)
                {
                    WPNewCommentModel *model = [WPNewCommentModel yy_modelWithDictionary:dict];
                    [self.models addObject:model];
                }
                
                //评论页数加1
                self.indexPage++;
                
                [self.bottomTableView.mj_footer endRefreshing];
                [self.bottomTableView reloadData];
            }
        }
        else
        {
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"加载失败"];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

#pragma mark - textView的代理方法
-(void)textViewDidChange:(UITextView *)textView
{
    if(textView.text.length)
    {
        self.sendBtn.enabled = YES;
    }
    else
    {
        self.sendBtn.enabled = NO;
    }
}

#pragma mark - 按钮的点击事件
-(void)praiseBtnDidClick:(UIButton *)btn
{
    if (btn.selected)
    {
        //        弹穿提示已经赞过
        [self showMessageLabelWith:@"亲, 已经赞过了~" font:[UIFont systemFontOfSize:13]];
        return ;
    }
    
    btn.selected = !btn.selected;
    self.model.isPraised = YES;
    self.model.praiseAmount++;
    self.praiseCount.text = [NSString stringWithFormat:@"%zd",self.model.praiseAmount];
    
    //    把评论的id传给用户
    NSString *url = @"appraise/praise.do";
    NSDictionary *dict = @{@"appraiseId":@(self.model.id)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //                NSLog(@"%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}


-(void)commentBtnDidClick:(UIButton *)btn
{
    if (![WPUserAccountViewModel sharedInstance].userLogin)
    {
        [[WPUserAccountViewModel sharedInstance] loginTipWith:^{
            
            WPMineLoginVC *vc = [WPMineLoginVC new];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
        }];
        return ;
    }
    
    if (btn)
    {
        self.isSpecialReply = NO;
    }
    else
    {
        self.isSpecialReply = YES;
    }
    
    //注册键盘监听
    NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
    //添加注册监听  self 监听键盘自动发出的UIKeyboardWillChangeFrameNotification通知，调用self的keyboardFrameChange:进行处理
    [center addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [center addObserver:self selector:@selector(keyboardHideFrameChange:) name:UIKeyboardWillHideNotification object:nil];
    
    [self.textView becomeFirstResponder];
}

//键盘监听处理方法
- (void) keyboardFrameChange:(NSNotification *)notice
{
    //1.获取动画时间
    CGFloat time=[notice.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect endRect=[notice.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat offset = 0;
    
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    offset = endRect.origin.y-SCREEN_HEIGHT-145;
    if(self.coverBtn == nil)
    {
        self.coverBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.coverBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.5);
        [self.coverBtn addTarget:self action:@selector(coverBtnDidClick) forControlEvents:UIControlEventTouchDown];
        [window addSubview:self.coverBtn];
    }
    
    [UIView animateWithDuration:time animations:^{
        
        CGRect frame = self.coverBtn.frame;
        frame.size.height = SCREEN_HEIGHT+offset;
        self.coverBtn.frame = frame;
        
        self.textViewBgView.transform=CGAffineTransformMakeTranslation(0, offset);
    }];
}

-(void)keyboardHideFrameChange:(NSNotification *)notice
{
    //1.获取动画时间
    CGFloat time=[notice.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [self.coverBtn removeFromSuperview];
    self.coverBtn = nil;
    
    [UIView animateWithDuration:time animations:^{
        
        self.textViewBgView.transform=CGAffineTransformMakeTranslation(0, 0);
        
    } completion:^(BOOL finished) {
    }];
}

-(void)coverBtnDidClick
{
    [self.textView resignFirstResponder];
    
//    WPLog(@"移除通知");
    NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
    [center removeObserver:self];
}

-(void)sendBtnDidClick:(UIButton *)btn
{
    if (self.textView.text.length > 500)
    {
        [self showErrorInfoWithStatus:@"字数超出限制长度"];
        return ;
    }
    
    NSString *url = @"appraise/addAppraise.do";
    NSString *content = self.textView.text;
    
    NSDictionary *value = nil;
    if (self.isSpecialReply)
    {
        WPNewCommentModel *model = self.models[self.selectedIndex];
        value = @{@"content":content,@"recordId":@(self.recordId),@"parentId":@(model.id)};
    }
    else
    {
        value = @{@"content":content,@"recordId":@(self.recordId),@"parentId":@(self.model.id)};
    }
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    //去除掉首尾的空白字符和换行字符
    jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSDictionary *dicts = @{@"json":jsonString};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dicts progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        
        if ([responseDict[@"result"] isEqualToString:@"success"])
        {
            
            self.textView.text = nil;
            [self.textView resignFirstResponder];
            
            [self refreshData];
        }
        else
        {
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"评论失败"];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
    
    self.isSpecialReply = NO;
}

//重新登陆的代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        WPMineLoginVC *vc = [WPMineLoginVC new];
        [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
    }
}

#pragma mark - 下拉上拉刷新
-(void)refreshData
{
    NSString *url = @"appraise/findAppraise.do";
    NSDictionary *dict = @{@"recordId":@(self.recordId),@"id":@(self.model.id),@"page":@(0)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        if ([responseDict[@"result"] isEqualToString:@"success"])
        {
            if ([responseObject[@"detail"] count])
            {
                self.bottomTableView.hidden = NO;
                
                [self.models removeAllObjects];
                for (NSDictionary *dict in responseDict[@"detail"])
                {
                    WPNewCommentModel *model = [WPNewCommentModel yy_modelWithDictionary:dict];
                    [self.models addObject:model];
                }
                
                //不满一页就隐藏
                if (self.models.count < 10)
                {
                    self.bottomTableView.mj_footer.hidden = YES;
                }
                else
                {
                    self.bottomTableView.mj_footer.hidden = NO;
                }
                
                
                self.indexPage = 1;
                [self.bottomTableView reloadData];
            }
            
            [self.bottomTableView.mj_header endRefreshing];
        }
        else
        {
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"加载失败"];
            }
            
            [self.bottomTableView.mj_header endRefreshing];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
        [self.bottomTableView.mj_header endRefreshing];
        
    }];
    
}

-(void)loadMoreData
{
    NSString *url = @"appraise/findAppraise.do";
    NSDictionary *dict = @{@"recordId":@(self.recordId),@"id":@(self.model.id),@"page":@(self.indexPage)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        if ([responseDict[@"result"] isEqualToString:@"success"])
        {
            NSArray *arr = responseDict[@"detail"];
            if (arr.count)
            {
                
                for (NSDictionary *dict in arr)
                {
                    WPNewCommentModel *model = [WPNewCommentModel yy_modelWithDictionary:dict];
                    [self.models addObject:model];
                }
                
                //不满一页就隐藏
                if (arr.count < 10)
                {
                    self.bottomTableView.mj_footer.hidden = YES;
                }
                else
                {
                    self.bottomTableView.mj_footer.hidden = NO;
                }
                
                self.indexPage++;
                [self.bottomTableView reloadData];
            }
            
            [self.bottomTableView.mj_footer endRefreshing];
        }
        else
        {
            [self dismiss];
            
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"加载失败"];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
        [self.bottomTableView.mj_footer endRefreshing];
    }];
    
}

-(void)showMessageLabelWith:(NSString *)title font:(UIFont *)font
{
    if (self.isShow)
    {
        return ;
    }
    UIButton *messageBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    [messageBtn setTitle:title forState:UIControlStateNormal];
    messageBtn.titleLabel.font = font;
    messageBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.6);
    messageBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [messageBtn sizeToFit];
    messageBtn.layer.cornerRadius = 3;
    messageBtn.layer.masksToBounds = YES;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:messageBtn];
    self.isShow = YES;
    messageBtn.center = CGPointMake(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.4);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:1 animations:^{
            
            messageBtn.alpha = 0;
            
        } completion:^(BOOL finished) {
            
            [messageBtn removeFromSuperview];
            self.isShow = NO;
        }];
        
    });
}


#pragma mark - tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPNewCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPNewCommentCell"];
    cell.isSubAppraise = YES;
    cell.enableReply = YES;
    cell.model = self.models[indexPath.item];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndex = indexPath.item;
    
    //回复单个评论
    [self commentBtnDidClick:nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"WPNewCommentCell" cacheByIndexPath:indexPath configuration:^(WPNewCommentCell *cell) {
        // 配置 cell 的数据源，和 "cellForRow" 干的事一致，比如：
        cell.model = self.models[indexPath.row];
    }];
}

-(NSMutableArray *)models
{
    if (_models == nil)
    {
        _models = [NSMutableArray array];
    }
    return _models;
}

-(void)backBtnDidClick
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)dealloc
{
    NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
    [center removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
